﻿using System;
using System.Windows.Forms;
using FastNET.CleanAct.Client.ManagerEngineBusiness.DataObjects;
using FastNET.CleanAct.Common.Repository;

namespace FastNET.CleanAct.Tools.HomeTemplateEditor.Forms
{
    internal partial class TemplateBox : Form
    {
        public TemplateBox()
        {
            InitializeComponent();

            PackageField.Items.Add(new { ID = new Identity(Guid.Empty), Name = "(User Generated)" });

            foreach (Package package in Package.Retrieve())
            {
                PackageField.Items.Add(package);
            }

            PackageField.DisplayMember = "Name";
            PackageField.SelectedIndex = 0;
        }

        public string TemplateName
        {
            get
            {
                return NameField.Text;
            }

            set
            {
                NameField.Text = value;
            }
        }

        public bool IsBuiltIn
        {
            get
            {
                return IsBuiltInField.Checked;
            }

            set
            {
                IsBuiltInField.Checked = value;
            }
        }

        public Identity? PackageID
        {
            get
            {
                if ((PackageField.SelectedItem != null) && (typeof(Package) == PackageField.SelectedItem.GetType()))
                {
                    return ((Package)PackageField.SelectedItem).ID;
                }

                return null;
            }

            set
            {
                if (value.HasValue)
                {
                    foreach (object item in PackageField.Items)
                    {
                        if (typeof(Package) != item.GetType())
                        {
                            continue;
                        }

                        if (((Package)item).ID != value.Value)
                        {
                            continue;
                        }

                        PackageField.SelectedItem = item;
                        return;
                    }
                }

                PackageField.SelectedIndex = 0;
            }
        }

    }
}
